﻿using System;
using System.ComponentModel;

namespace TeacherLeftHand.Model
{
    /// <summary>
    /// Class represent a knowledge item aka kiến thức
    /// </summary>
    public class KnowledgeItem : IDataErrorInfo, IData
    {
        #region Const
        public const string Practical = "Lý thuyết";
        public const string Theory = "Thực hành";
        #endregion

        #region Properties

        private int _id;
        private string _content;
        private int _classLevel;
        private string _subject;
        private string _topic;
        private int _topicNo;
        private string _kiType;

        /// <summary>
        /// Get or set KI Content
        /// </summary>
        public string Content
        {
            get
            {
                return _content;
            }
            set
            {
                _content = value;
            }
        }

        /// <summary>
        /// Get or set KI class level
        /// </summary>
        public int ClassLevel { get { return _classLevel; } set { _classLevel = value; } }

        /// <summary>
        /// Get or set KI subject
        /// </summary>
        public string Subject { get { return _subject; } set { _subject = value; } }

        /// <summary>
        /// Get or set KI Topic
        /// </summary>
        public string Topic { get { return _topic; } set { _topic = value; } }

        /// <summary>
        /// Get or set KI type
        /// </summary>
        public string KIType { get { return _kiType; } set { _kiType = value; } }

        /// <summary>
        /// Get KI ID
        /// </summary>
        public int ID { get { return _id; } set { _id = value; } }

        /// <summary>
        /// Get or set Knowledge item topic number
        /// </summary>
        public int TopicNo
        {
            get { return _topicNo; }
            set { _topicNo = value; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Knowledge Item's Constructor 
        /// </summary>
        /// /// <param name="id">ID of the KI</param>
        /// <param name="content">Content of the KI</param>
        /// <param name="classLevel">Class Level : from 1 - 5</param>
        /// <param name="subject">Subject : fixed from a pre-defined list</param>
        /// <param name="topic">Topic : fixed from a pre-defined list</param>
        /// <param name="topicNo"> </param>
        /// <param name="kitype"> </param>
        public KnowledgeItem(int id = -1,string content = "", int classLevel = 1, string subject = "", 
                             string topic = "", int topicNo = 1, string kitype = Practical)
        {
            _id = id;
            Content = content;
            ClassLevel = classLevel;
            Subject = subject;
            Topic = topic;
            TopicNo = topicNo;
            KIType = kitype;
        }

        public KnowledgeItem(KnowledgeItem item)
        {
            ID = item.ID;
            Content = item.Content;
            ClassLevel = item.ClassLevel;
            Subject = item.Subject;
            Topic = item.Topic;
            TopicNo = item.TopicNo;
            KIType = item.KIType;
        }

        public KnowledgeItem ()
        {
            ID = -1;
            Content = "";
            ClassLevel = 0;
            Subject = "";
            Topic = "";
            TopicNo = 0;
            KIType = " ";
        }
        #endregion

        #region Methods
        public override string ToString()
        {
            return Content;
        }

        #endregion

        #region Implementation of IDataErrorInfo

        /// <summary>
        /// Gets the error message for the property with the given name.
        /// </summary>
        /// <returns>
        /// The error message for the property. The default is an empty string ("").
        /// </returns>
        /// <param name="columnName">The name of the property whose error message to get. </param>
        public string this[string columnName]
        {
            get
            {
                string msg = string.Empty;
                switch (columnName)
                {
                    // Content is required field
                    case "Content":
                        if (string.IsNullOrEmpty(Content))
                        {
                            msg = "Required field";
                        }
                        break;

                    // class level is required field and in range [1,5]
                    case "ClassLevel":
                        if (ClassLevel > 5 || ClassLevel < 1)
                        {
                            msg = "Required field";
                        }
                        break;

                    //subject is required field
                    case "Subject":
                        if (string.IsNullOrEmpty(Subject))
                        {
                            msg = "Required field";
                        }
                        break;

                    // topic is required field
                    case "Topic":
                        if (string.IsNullOrEmpty(Topic))
                        {
                            msg = "Required field";
                        }
                        break;
                    // other are optional with no constraints
                    case "ID":
                    case "KIType":
                    case "TopicNo":
                        break;
                    default:
                        throw new ArgumentException(
                        "Unrecognized property: " + columnName);
                }
                return msg;
            }
        }

        /// <summary>
        /// Gets an error message indicating what is wrong with this object.
        /// </summary>
        /// <returns>
        /// An error message indicating what is wrong with this object. The default is an empty string ("").
        /// </returns>
        public string Error
        {
            get { return string.Empty; }
        }
        #endregion
    }
}
